package com.example.springboot.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.entity.User_Data;
import com.example.springboot.mapper.UserDataMapper;
import org.springframework.stereotype.Service;

/**
 * 功能：
 * 作者：邓盛典
 * 日期：2024/8/25 7:56
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDataMapper, User_Data> implements UserService {

    @Override
    public User_Data login(User_Data user) {
        User_Data dbUser = lambdaQuery().eq(User_Data::getUsername, user.getUsername()).one();
        if (dbUser == null) {
            throw new RuntimeException("用户名或密码错误");
        }
        if (!user.getPassword().equals(dbUser.getPassword())) {
            throw new RuntimeException("用户名或密码错误");
        }
        return dbUser;
    }

    @Override
    public User_Data register(User_Data user) {
        User_Data existingUser = lambdaQuery().eq(User_Data::getUsername, user.getUsername()).one();
        if (existingUser!= null) {
            throw new RuntimeException("用户名已存在");
        }
        save(user);
        return user;
    }
}